package com.gosling.cloudspace.controller;

import com.gosling.cloudspace.common.entity.dtos.TestDto;
import com.gosling.cloudspace.event.CustomEventPublisher;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@Slf4j
@RequestMapping(value = "/test")
@RestController
//@Scope("prototype")
public class TestController {
    private int var = 0;
    @Autowired
    private CustomEventPublisher customEventPublisher;

    @GetMapping(value = "/test_var")
    public String test() {
        log.info("普通变量var:" + (++var));
        return "普通变量var:" + var;
    }

    @GetMapping(value = "/test2")
    public String test2() {
        log.info("普通变量var:" + (++var));
        return "普通变量var:" + var;
    }

    @GetMapping(value = "/test3")
    public String test3() {
        customEventPublisher.publishCustomEvent("测试事件触发...");
        return "处理完成";
    }

    @PostMapping(value = "/test4")
    public String test4(@Valid @RequestBody TestDto vo) {
        System.out.println(vo.getName());
        return "处理完成";
    }

    @PostMapping(value = "/test5")
    public String test5(@Valid @RequestBody String vo) {
        System.out.println(vo);
        throw new RuntimeException("测试异常");
    }
}
